#include <iostream>

#include <functional>

#include <nemesis.h>

using namespace std;

struct Accesor {
   static int value (int* i) throw () { return *i; }
   static int value (const int* i) throw () { return *i; }
};

void run () 
   throw (RuntimeException)
{

   int values [] = { 
      49800, 49801, 49802, 57000, 49804, 49805, 49806, 62000, 102400, 49806, 62001, 49808,
      102401, 67400, 49809, 71100, 49700, 63900,  49810, 67420, 67300, -1
   };

   SortedVector <int, Accesor> sorted;
   typedef SortedVector <int, Accesor>::iterator iterator;

   for (int i = 0; values [i] != -1; i ++) 
      sorted.add (&values [i]);

   for (iterator ii = sorted.begin (), maxii = sorted.end (); ii != maxii; ii ++) 
       cout << functions::asString (Accesor::value (SortedVector <int, Accesor>::data (ii))) << " ";
   cout << endl << endl;

   bool found;
   string msg;
   int value;

   for (int i = 0; values [i] != -1; i ++) {
     value = values [i] - 1;
     found = sorted.contains (&value);
     msg = functions::asText ("Numero-: ", value);
     msg += functions::asText (" | Existe: ", found);
     Logger::debug (msg, FILE_LOCATION);
   
     found = sorted.contains (&values [i]);
     msg = functions::asText ("Clave: ", values [i]);
     msg += functions::asText (" | Existe: ", found);
     Logger::debug (msg, FILE_LOCATION);
  
     value = values [i] + 1;
     found = sorted.contains (&value);
     msg = functions::asText ("Numero+: ", value);
     msg += functions::asText (" | Existe: ", found);
     Logger::debug (msg, FILE_LOCATION);
   }
   
   for (int loop = 0; loop < 500; loop ++) {
      for (int i = 0; values [i] != -1; i ++) {
        value = values [i] - 1;
        found = sorted.contains (&value);
   
        found = sorted.contains (&values [i]);
   
        value = values [i] + 1;
        found = sorted.contains (&value);
      }
   }
}

int main (int argc, const char** argv)
{
   try {
      Logger::setLevel (Logger::Debug); 
      Logger::initialize ("testfunctions", new TraceWriter ("file.trace", 2048000));
  
      run ();
   }
   catch (Exception& ex) {
      cout << ex.asString () << endl;
   }
   
   return 0;
}

